package com.dd.cloud.user.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.cloud.common.vo.PageVo;
import com.dd.cloud.user.entity.admin.AdminSetDep;
import com.dd.cloud.user.req.admin.*;
import com.dd.cloud.user.req.mechan.MechanRankPresStatisticalReq;
import com.dd.cloud.user.req.mechan.MechanRankSelectStatisticalReq;
import com.dd.cloud.user.res.admin.*;
import com.dd.cloud.user.res.mechan.MechanRankPresRes;
import com.dd.cloud.user.res.mechan.MechanRankSelectRes;
import com.dd.cloud.user.service.admin.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 管理员用户 前端控制器
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@RestController
@RequestMapping("/admin")
@Validated
public class AdminController {
    @Autowired
    private IAdminUserService adminUserService;
    @Autowired
    private IContactUsService contactUsService;
    @Autowired
    private IAdminSetDepService adminSetDepService;
    @Autowired
    private IAdminSaleInfoService adminSaleInfoService;
    @Autowired
    private ISaleInviteDoctorService saleInviteDoctorService;
    @Autowired
    private ISaleInviteMechanService saleInviteMechanService;
    @Autowired
    private ISaleInviteAgentService saleInviteAgentService;

    /**
     * 机构管理员用户登录 验证码
     *
     * @param request  请求对象
     * @param response 响应对象
     * @param phone    手机号
     * @param code     验证码
     * @return
     */
    @PostMapping("/user/login/code")
    public ResponseEntity<AdminUserRes> merchantUserLoginCode(HttpServletRequest request, HttpServletResponse response, @NotBlank(message = "手机号不能为空") String phone, @NotBlank(message = "验证码不能为空") String code) {
        AdminUserRes res = adminUserService.adminUserLoginCode(request, response, phone, code);
        return ResponseEntity.ok(res);
    }

    /**
     * 机构管理员用户登录 密码
     *
     * @param request
     * @param response
     * @param account
     * @param password
     * @return
     */
    @PostMapping("/user/login/pwd")
    public ResponseEntity<AdminUserRes> adminUserLoginPwd(HttpServletRequest request, HttpServletResponse response, @NotBlank(message = "账号不能为空") String account, @NotBlank(message = "密码不能为空") String password) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (String paramName : parameterMap.keySet()) {
            String[] paramValues = parameterMap.get(paramName);
            for (String paramValue : paramValues) {
                System.out.println(paramName + " : " + paramValue);
            }
        }
        AdminUserRes res = adminUserService.adminUserLoginPwd(request, response, account, password);
        return ResponseEntity.ok(res);
    }

    /**
     * 机构管理员用户修改密码
     *
     * @param oldPwd
     * @param newPwd1
     * @param newPwd2
     */
    @PostMapping("/user/update/pwd")
    public ResponseEntity<Void> updateMerchantUserPwd(@NotBlank(message = "原密码不能为空") String oldPwd, @NotBlank(message = "新密码不能为空") String newPwd1, @NotBlank(message = "重复密码不能为空") String newPwd2) {
        adminUserService.adminUserPwd(oldPwd, newPwd1, newPwd2);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加管理员用户
     *
     * @param req
     */
    @PostMapping("/user/add")
    public ResponseEntity<Void> addAdminUser(@Valid @RequestBody AddAdminUserReq req) {
        adminUserService.addAdminUser(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改管理员用户
     *
     * @param req
     */
    @PostMapping("/user/update")
    public ResponseEntity<Void> updateAdminUser(@Valid @RequestBody UpdateAdminUserReq req) {
        adminUserService.updateAdminUser(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改管理员用户状态
     *
     * @param id
     */
    @PostMapping("/user/update/status")
    public ResponseEntity<Void> updateAdminUserStatus(@NotNull(message = "管理员id不能为空") Integer id, @NotNull(message = "管理员状态不能为空") Integer status) {
        adminUserService.updateAdminUserStatus(id, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 分页查询管理员
     *
     * @param req
     */
    @GetMapping("/user/page")
    public ResponseEntity<Page<AdminUserRes>> getUserPage(@Valid GetAdminPageReq req) {
        Page<AdminUserRes> res = adminUserService.getAdminUserPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询管理员详情
     *
     * @param id
     */
    @GetMapping("/user/detail")
    public ResponseEntity<AdminUserDetailRes> getUserPage(@NotNull(message = "管理员id不能为空") Integer id) {
        AdminUserDetailRes res = adminUserService.getAdminUserDetail(id);
        return ResponseEntity.ok(res);
    }


    /**
     * 查询联系我们
     */
    @GetMapping("/contact/page")
    public ResponseEntity<List<ContactUsRes>> getContactPage() {
        List<ContactUsRes> res = contactUsService.getContactUs();
        return ResponseEntity.ok(res);
    }

    /**
     * 修改联系我们
     *
     * @param req
     */
    @PostMapping("/contact/update")
    public ResponseEntity<Void> updateContact(@Valid UpdateContactUsReq req) {
        contactUsService.updateContactUs(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询当前管理员能配置的科室
     */
    @GetMapping("/setdep/list")
    public ResponseEntity<List<AdminSetDep>> getAdminSetDepList() {
        List<AdminSetDep> res = adminSetDepService.getAdminSetDepList();
        return ResponseEntity.ok(res);
    }

    /**
     * 统计,查询各种客户端用户数量
     */
    @GetMapping("/statistical/user/num")
    public ResponseEntity<StatisticalUserNumRes> getStatisticalUserNum() {
        StatisticalUserNumRes res = adminUserService.getStatisticalUserNum();
        return ResponseEntity.ok(res);
    }

    /**
     * 统计,查询机构的地址及数量
     */
    @GetMapping("/statistical/mechan/addr")
    public ResponseEntity<Map<String, List<StatisticalMechanAddrRes>>> getStatisticalMechanAddr() {
        Map<String, List<StatisticalMechanAddrRes>> res = adminUserService.getStatisticalMechanAddr();
        return ResponseEntity.ok(res);
    }

    /**
     * 统计,查询机构的地址及数量
     */
    @GetMapping("/statistical/mechan/region")
    public ResponseEntity<List<StatisticalMechanRegionRes>> getStatisticalMechanRegion() {
        List<StatisticalMechanRegionRes> res = adminUserService.getStatisticalMechanRegion();
        return ResponseEntity.ok(res);
    }

    /**
     * 统计,查询机构的处方数量
     *
     * @param timeType today当天 yesterday昨天 thisMonth本月
     */
    @GetMapping("/statistical/mechan/pres")
    public ResponseEntity<List<StatisticalMechanPresRes>> getStatisticalMechanPres(String timeType) {
        List<StatisticalMechanPresRes> res = adminUserService.getStatisticalMechanPres(timeType);
        return ResponseEntity.ok(res);
    }

    /**
     * 统计,查询机构的类型及数量
     */
    @GetMapping("/statistical/mechan/type")
    public ResponseEntity<List<StatisticalMechanTypeRes>> getStatisticalMechanType() {
        List<StatisticalMechanTypeRes> res = adminUserService.getStatisticalMechanType();
        return ResponseEntity.ok(res);
    }

    /**
     * 统计,各个时间维度查询机构的处方数量
     */
    @GetMapping("/statistical/mechan/node")
    public ResponseEntity<List<StatisticalNodeRes>> getStatisticalMechanDateNode(@Valid StatisticalMechanDateNodeReq req) {
        List<StatisticalNodeRes> res = adminUserService.getStatisticalMechanDateNode(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 统计,查询机构的select框列表
     */
    @GetMapping("/statistical/mechan/select")
    public ResponseEntity<List<MechanRankSelectRes>> getStatisticalMechanSelect(@Valid MechanRankSelectStatisticalReq req) {
        List<MechanRankSelectRes> res = adminUserService.getMechanSelect(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 统计,查询机构的开放数量统计
     */
    @GetMapping("/statistical/mechan/select/pres")
    public ResponseEntity<List<MechanRankPresRes>> getStatisticalMechanSelectPres(@Valid MechanRankPresStatisticalReq req) {
        List<MechanRankPresRes> res = adminUserService.getMechanPresDay(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 重置管理员密码
     *
     * @param adminId
     */
    @PostMapping("/user/reset/pwd")
    public ResponseEntity<Void> resetAdminUserPassword(@NotNull(message = "管理员/销售id不能为空") Integer adminId) {
        adminUserService.resetAdminUserPassword(adminId);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询管理员销售列表
     *
     * @param req
     * @return
     */
    @GetMapping("/sale/page")
    public ResponseEntity<IPage<AdminSaleInfoPageRes>> queryAdminSaleInfoPage(@Valid AdminSaleInfoPageReq req) {
        IPage<AdminSaleInfoPageRes> res = adminSaleInfoService.queryAdminSaleInfoPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 创建销售管理员
     *
     * @param req
     */
    @PostMapping("/sale/create")
    public ResponseEntity<Void> createAdminSaleInfo(@Valid @RequestBody AdminSaleInfoCreateReq req) {
        adminSaleInfoService.createAdminSaleInfo(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改销售管理员
     *
     * @param req
     */
    @PostMapping("/sale/update")
    public ResponseEntity<Void> updateAdminSaleInfo(@Valid @RequestBody AdminSaleInfoUpdateReq req) {
        adminSaleInfoService.updateAdminSaleInfo(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 销售人员交接客户数据
     *
     * @param beId 要交接人员的id
     * @param toId 接收的人员id
     */
    @PostMapping("/sale/join")
    public ResponseEntity<Void> saleJoin(Integer beId, Integer toId) {
        adminSaleInfoService.saleJoin(beId, toId);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询销售管理员详情
     *
     * @param saleId
     * @return
     */
    @GetMapping("/sale/detail")
    public ResponseEntity<AdminSaleInfoDetailRes> queryAdminSaleInfoDetail(Integer saleId) {
        AdminSaleInfoDetailRes res = adminSaleInfoService.queryAdminSaleInfoDetail(saleId);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询销售邀请医生列表
     *
     * @param req
     * @return
     */
    @GetMapping("/sale/invite/doctor/page")
    public ResponseEntity<PageVo<SaleInviteDoctorPageRes>> querySaleInviteDoctorPage(@Valid SaleInviteDoctorPageReq req) {
        PageVo<SaleInviteDoctorPageRes> res = saleInviteDoctorService.querySaleInviteDoctorPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 销售统计邀请已认证医生开方数据
     *
     * @param req
     * @return
     */
    @GetMapping("/sale/invite/doctor/statistics/auth/page")
    public ResponseEntity<PageVo<SaleInviteDoctorStatisticsAuthPageRes>> querySaleInviteDoctorStatisticsAuthPage(@Valid SaleInviteDoctorStatisticsAuthPageReq req) {
        PageVo<SaleInviteDoctorStatisticsAuthPageRes> res = saleInviteDoctorService.querySaleInviteDoctorStatisticsAuthPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 销售统计邀请未认证医生开方数据
     *
     * @param req
     * @return
     */
    @GetMapping("/sale/invite/doctor/statistics/no/auth/page")
    public ResponseEntity<PageVo<SaleInviteDoctorStatisticsNoAuthPageRes>> querySaleInviteDoctorStatisticsNoAuthPage(@Valid SaleInviteDoctorStatisticsNoAuthPageReq req) {
        PageVo<SaleInviteDoctorStatisticsNoAuthPageRes> res = saleInviteDoctorService.querySaleInviteDoctorStatisticsNoAuthPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 销售查询邀请医生订单列表
     *
     * @param req
     * @return
     */
    @GetMapping("/sale/invite/doctor/order/page")
    public ResponseEntity<IPage<SaleInviteDoctorOrderPageRes>> querySaleInviteDoctorOrderPage(@Valid SaleInviteDoctorOrderPageReq req) {
        IPage<SaleInviteDoctorOrderPageRes> res = saleInviteDoctorService.querySaleInviteDoctorOrderPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询销售邀请机构列表
     *
     * @param req
     * @return
     */
    @GetMapping("/sale/invite/mechan/page")
    public ResponseEntity<PageVo<SaleInviteMechanPageRes>> querySaleInviteMechanPage(SaleInviteMechanPageReq req) {
        PageVo<SaleInviteMechanPageRes> res = saleInviteMechanService.querySaleInviteMechanPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询销售邀请代理商列表
     *
     * @param req
     * @return
     */
    @GetMapping("/sale/invite/agent/page")
    public ResponseEntity<PageVo<SaleInviteAgentPageRes>> querySaleInviteAgentPage(SaleInviteAgentPageReq req) {
        PageVo<SaleInviteAgentPageRes> res = saleInviteAgentService.querySaleInviteAgentPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 销售首页, 查询邀请医生统计
     * 查询销售邀请代理商列表
     *
     * @param req
     * @return
     */
    @GetMapping("/sale/invite/doctor/statistics")
    public ResponseEntity<SaleInviteDoctorStatisticsRes> querySaleDoctorStatistics(SaleInviteDoctorStatisticsReq req) {
        SaleInviteDoctorStatisticsRes res = saleInviteDoctorService.querySaleDoctorStatistics(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 销售首页, 查询邀请机构统计
     * 查询销售邀请代理商列表
     *
     * @param req
     * @return
     */
    @GetMapping("/sale/invite/mechan/statistics")
    public ResponseEntity<SaleInviteDoctorStatisticsRes> querySaleMechanStatistics(SaleInviteDoctorStatisticsReq req) {
        SaleInviteDoctorStatisticsRes res = saleInviteMechanService.querySaleMechanStatistics(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 销售首页, 查询邀请代理商统计
     * 查询销售邀请代理商列表
     *
     * @param req
     * @return
     */
    @GetMapping("/sale/invite/agent/statistics")
    public ResponseEntity<SaleInviteDoctorStatisticsRes> querySaleAgentStatistics(SaleInviteDoctorStatisticsReq req) {
        SaleInviteDoctorStatisticsRes res = saleInviteAgentService.querySaleAgentStatistics(req);
        return ResponseEntity.ok(res);
    }
}

